package com.amigo.online.provider.sync.handle.data.mongo.service.impl;

import java.util.List;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.common.ConstantValue;
import com.amigo.online.provider.common.entity.VideoMongoInfo;
import com.amigo.online.provider.sync.common.AbstractGlobalService;
import com.amigo.online.provider.sync.handle.data.mongo.service.VideoMongoService;
import com.amigo.online.provider.sync.mongo.entity.VideoEntity;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;


@Service
public class VideoMongoServiceImpl extends AbstractGlobalService implements VideoMongoService {

    @Autowired
    @Qualifier("videoMongoTemplate")
    MongoTemplate mongoTemplate;

    @Override
    public void insertObject(Document video) {
        mongoTemplate.getCollection(ConstantValue.VIDEO_MONGO).insertOne(video);
    }

    @Override
    public long updateObject(Query query, Update update) {
        UpdateResult result = mongoTemplate.updateFirst(query, update, ConstantValue.VIDEO_MONGO);
        if (result != null) {
            logger.info("video mongo 更新结果" + result.toString());
            return result.getModifiedCount();
        } else {
            return 0L;
        }
    }

    @Override
    public long deleteObject(Query query) {
        DeleteResult result = mongoTemplate.remove(query, ConstantValue.VIDEO_MONGO);
        if (result != null) {
            logger.info("video mongo 删除结果" + result.toString());
            return result.getDeletedCount();
        } else {
            return 0L;
        }
    }

    @Override
    public List<VideoEntity> listVideoByIds(List<Long> ids) {
        Query query = new Query().addCriteria(Criteria.where("_id").in(ids));
        return mongoTemplate.find(query, VideoEntity.class, ConstantValue.VIDEO_MONGO);
    }

    @Override
    public List<VideoMongoInfo> listVideoByStartIdAndEndId(int startId, int endId) {
        Query query = new Query().addCriteria(Criteria.where("_id").gte(startId).lte(endId));
        return mongoTemplate.find(query, VideoMongoInfo.class, ConstantValue.VIDEO_MONGO);
    }

    @Override
    public List<VideoMongoInfo> listVideoMongoInfoByIds(List<Long> ids) {
        Query query = new Query().addCriteria(Criteria.where("_id").in(ids));
        return mongoTemplate.find(query, VideoMongoInfo.class, ConstantValue.VIDEO_MONGO);
    }

}
